<template>
    <el-dialog v-model="props.visible" @closed="handle">
        <el-form label-width="120px" v-if="change === 'add' ? true : false">
            <el-form-item label="账号">
                <el-input type="text" v-model="form.username" placeholder="请输入账号" />
            </el-form-item>
            <el-form-item label="姓名">
                <el-input type="text" v-model="form.nickName" placeholder="请输入姓名" />
            </el-form-item>
            <el-form-item label="邮箱">
                <el-input v-model="form.email" placeholder="请输入邮箱" />
            </el-form-item>
            <el-form-item label="密码">
                <el-input type="password" v-model="form.password" placeholder="请输入密码" />
            </el-form-item>
            <el-form-item label="备注">
                <el-input type="textarea" v-model="form.note" placeholder="请输入备注" />
            </el-form-item>
            <el-form-item label="是否启用">
                <el-radio-group v-model="form.status" class="ml-4">
                    <el-radio label="1" size="large">是</el-radio>
                    <el-radio label="2" size="large">否</el-radio>
                </el-radio-group>
            </el-form-item>
            <el-form-item>
                <el-button @click="onCancel">取消</el-button>
                <el-button @click="adduser">确定</el-button>
            </el-form-item>
        </el-form>
        <div v-else>
            <el-dropdown @command="handleCommand">
                <span class="el-dropdown-link">
                    <el-tag v-for="tag in dynamicTags" closable :key="tag" @close="handleClose(tag)">
                        {{ tag }}
                    </el-tag>
                    <el-icon class="el-icon--right"><arrow-down /></el-icon>

                </span>
                <template #dropdown>
                    <el-dropdown-menu>
                        <el-dropdown-item command="a">超级管理员</el-dropdown-item>
                        <el-dropdown-item command="b">超级CEO</el-dropdown-item>
                        <el-dropdown-item command="c">Action 3</el-dropdown-item>
                        <el-dropdown-item command="d">Action 4</el-dropdown-item>
                        <el-dropdown-item command="e">Action 5</el-dropdown-item>
                    </el-dropdown-menu>
                </template>

            </el-dropdown>

            <!-- <el-select v-model="value" class="m-2" placeholder="Select" size="large">
                <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value" />
            </el-select> -->
        </div>
    </el-dialog>
</template>

<script setup lang="ts">
import { defineProps, watch, ref, reactive, withDefaults, defineEmits } from 'vue'
import { ArrowDown } from '@element-plus/icons-vue'
import { ElMessage, ElMessageBox, affixProps } from 'element-plus'
import { IAdminListItem } from "../../request/QmsAdminConreiller/index"
import type { Ref } from 'vue'
import * as api from '../../request/api'
let props = withDefaults(
    defineProps<{
        visible: boolean;
        change: string;
        
    }>(), {
    visible: true,
    change: 'add'
})
const emit = defineEmits(["getValue"])
const handle = () => {
    // console.log(emit);

    emit("getValue", false)
}

//表单参数
const originform = ref({}) as Ref<IAdminListItem>
const form = ref({}) as Ref<IAdminListItem>

const onCancel = () => {
    //数据传输
    form.value = { ...originform.value }
    // emit("getValue", false)
}
const adduser = () => {
    //数据传输
    console.log(form.value);
    ElMessageBox.confirm(
        '确定添加嘛',
        '添加提示',
        {
            confirmButtonText: '确认',
            cancelButtonText: '取消',
            type: 'warning',
        }
    )
        .then(async () => {
            await api.admin.adminRegister(form.value)
            ElMessage({
                type: 'success',
                message: '提交成功',
            })
            emit("getValue", false)
        })
        .catch(() => {
            ElMessage({
                type: 'info',
                message: '取消成功',
            })
        })

}

//分配
const handleCommand = (command: any) => {
    // ElMessage(`click on item ${command}`)
    console.log(command);
    dynamicTags.value.push(command)
}

const dynamicTags = ref(['Tag 1', 'Tag 2', 'Tag 3'])

const handleClose = (tag: string) => {
    dynamicTags.value.splice(dynamicTags.value.indexOf(tag), 1)
}





const value = ref('')

const options = [
    {
        value: 'Option1',
        label: 'Option1',
    },
    {
        value: 'Option2',
        label: 'Option2',
    },
    {
        value: 'Option3',
        label: 'Option3',
    },
    {
        value: 'Option4',
        label: 'Option4',
    },
    {
        value: 'Option5',
        label: 'Option5',
    },
]
</script>

<style lang="scss" scoped>
.el-dropdown-link {
    width: 200px;
    padding: 5px;
    border: 2px solid #ccc;

    .el-tag {
        margin: 2px;
    }

    .el-icon {
        float: right;
    }
}
</style>